<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0,shrink-to-fit=no, viewport-fit=cover">
    <meta name="keywords" content="蓝桥到家">
    <meta name="description" content="生鲜副食、饮品乳品、鲜花蛋糕">
    <title>注册</title>
    <link rel="stylesheet" href="../styles/reset.css">
    <link rel="stylesheet" href="../styles/login.css">
    <link rel="stylesheet" href="../styles/common.css">
    <script src="../js/common.js"></script>
    <script src="../js/zepto.min.js"></script>
</head>
<body>
    <div class="wrap">
        <div class="container">
            <div class="header_logo">
                <img src="https://static.shiyanlou.com/lanqiao/frontend/dist/img/lanqiao-logo.1bbdf85.svg" alt="logo">
            </div>

            <div class="login_item">
                <span class="login_text">手机号</span>
                <input class="login_input reg-phone" type="tel" maxlength="11" placeholder="输入手机号" />
            </div>
            <div class="login_item">
                <span class="login_text">验证码</span>
                <div class="auth_div">
                    <input class="login_input reg-code" type="text" maxlength="4" placeholder="输入验证码" />
                    <div class="code" id="send-code" style="background-color: #007bff; color: white; padding: 5px; text-align: center; cursor: pointer;">
                        <span>获取验证码</span>
                    </div>
                </div>
            </div>
            <div class="login_item">
                <span class="login_text">密码</span>
                <input class="login_input reg-pwd" type="password" maxlength="11" placeholder="设置您的登录密码" />
            </div>
            <div class="login_button">
                <a href="javascript:;" class="btn-register"><span>注册</span></a>
            </div>
            <div class="register_div">
                <a href="login.html" class="register_text">已有账号，请登录</a>
            </div>
        </div>
    </div>

    <script>
        $(function () {
            let countdown = 60; // 倒计时秒数
            let countdownInterval; // 倒计时定时器
            let randomCode; // 随机验证码
            let isCooldown = false; // 冷却状态标志

            // 验证手机号格式
            function isValidPhone(phone) {
                return /^1\d{10}$/.test(phone);
            }

            // 生成随机验证码
            function generateRandomCode() {
                return Math.floor(1000 + Math.random() * 9000).toString();
            }

            // 手机号输入框失去焦点时验证格式
            $('.reg-phone').on('blur', function () {
                const phone = $(this).val().trim();
                if (!isValidPhone(phone)) {
                    alert('请输入有效的手机号');
                }
            });

            // 获取验证码按钮点击事件
            $('#send-code').on('click', function () {
                if (isCooldown) {
                    alert('验证码正在冷却中，请稍后再试');
                    return;
                }

                const phone = $('.reg-phone').val().trim();
                if (!phone) {
                    alert('请输入手机号');
                    return;
                }
                if (!isValidPhone(phone)) {
                    alert('请输入有效的手机号');
                    return;
                }

                // 生成随机验证码
                randomCode = generateRandomCode();

                // 模拟发送验证码
                alert('验证码已发送，请注意查收，验证码为：' + randomCode);
                $(this).find('span').text('60秒后重试');
                $(this).css('background-color', '#ccc'); // 设置为灰色
                $(this).css('cursor', 'default'); // 设置为默认光标
                isCooldown = true; // 设置冷却状态

                // 开始倒计时
                countdownInterval = setInterval(function () {
                    countdown--;
                    if (countdown <= 0) {
                        clearInterval(countdownInterval);
                        $('#send-code').find('span').text('获取验证码');
                        $('#send-code').css('background-color', '#007bff'); // 恢复为蓝色
                        $('#send-code').css('cursor', 'pointer'); // 恢复为可点击光标
                        countdown = 60; // 重置倒计时
                        isCooldown = false; // 重置冷却状态
                    } else {
                        $('#send-code').find('span').text(countdown + '秒后重试');
                    }
                }, 1000);
            });

            // 注册按钮点击事件
            $('.btn-register').on('click', function () {
                const phone = $('.reg-phone').val().trim();
                const code = $('.reg-code').val().trim();
                const password = $('.reg-pwd').val().trim();

                if (!phone || !code || !password) return alert('请完整填写信息');
                if (!isValidPhone(phone)) return alert('请输入有效的手机号');
                if (code !== randomCode) return alert('验证码错误');

                $.getJSON('http://localhost:3000/users?phone=' + phone, function (users) {
                    if (users.length) {
                        alert('该手机号已注册');
                        return;
                    }

                    $.ajax({
                        url: 'http://localhost:3000/users',
                        type: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify({ phone, code, password }),
                        success: function () {
                            alert('注册成功，请登录');
                            location.href = 'login.html';
                        },
                        error: function () {
                            alert('注册失败，请重试');
                        }
                    });
                });
            });
        });
    </script>
</body>
</html>